package com.github.xzlink.loop;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by symark on 2016/11/30.
 */
public class FlvWriter {

    private String path;
    private String name;
    private FileOutputStream out;

    public FlvWriter(String path,String name){
        this.path = path;
        this.name = name;
        File file  = new File(path,name+".flv");
        if(file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try{
            out = new FileOutputStream(file);
            out.write("FLV".getBytes());
            out.write(0x01);
            out.write(0x01);
            out.write(0x00);
            out.write(0x00);
            out.write(0x00);
            out.write(0x09);
            //previous tag size 0
            out.write(0x00);
            out.write(0x00);
            out.write(0x00);
            out.write(0x00);



            byte[] demo = new byte[]{0x12,0x00,0x00,(byte)0xD4,0x00,0x00,0x00,0x00,0x00,0x00,0x00
                    ,0x02,0x00,0x0A,0x6F,0x6E,0x4D,0x65,0x74,0x61,0x44,0x61,0x74,0x61,0x08
                    ,0x00,0x00,0x00,0x09,0x00,0x08,0x64,0x75,0x72,0x61,0x74,0x69,0x6F,0x6E
                    ,0x00,0x40,0x32,(byte)0xEB,(byte)0x85,0x1E,(byte)0xB8,0x51,(byte)0xEC,0x00,0x05,0x77,0x69,0x64
                    ,0x74,0x68,0x00,0x40,(byte)0x9E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x68
                    ,0x65,0x69,0x67,0x68,0x74,0x00,0x40,(byte)0x90,(byte)0xE0,0x00,0x00,0x00,0x00,0x00
                    ,0x00,0x0D,0x76,0x69,0x64,0x65,0x6F,0x64,0x61,0x74,0x61,0x72,0x61,0x74
                    ,0x65,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09,0x66,0x72
                    ,0x61,0x6D,0x65,0x72,0x61,0x74,0x65,0x00,0x40,0x39,0x00,0x00,0x00,0x00
                    ,0x00,0x00,0x00,0x0C,0x76,0x69,0x64,0x65,0x6F,0x63,0x6F,0x64,0x65,0x63
                    ,0x69,0x64,0x00,0x40,0x1C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x05,0x74
                    ,0x69,0x74,0x6C,0x65,0x02,0x00,0x12,0x4D,0x65,0x64,0x69,0x61,0x20,0x50
                    ,0x72,0x65,0x73,0x65,0x6E,0x74,0x61,0x74,0x69,0x6F,0x6E,0x00,0x07,0x65
                    ,0x6E,0x63,0x6F,0x64,0x65,0x72,0x02,0x00,0x0D,0x4C,0x61,0x76,0x66,0x35
                    ,0x37,0x2E,0x35,0x31,0x2E,0x31,0x30,0x33,0x00,0x08,0x66,0x69,0x6C,0x65
                    ,0x73,0x69,0x7A,0x65,0x00,0x41,0x44,0x50,(byte)0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x09};

//            int scriptDataLength = 0;
//            byte[] scriptData = new byte[128];
//            scriptData[0] = 0x12;
//            scriptData[1] = (byte)((scriptDataLength>>16)&0xff);
//            scriptData[2] = (byte)((scriptDataLength>>8)&0xff);
//            scriptData[3] = (byte)((scriptDataLength)&0xff);
//            //TimeStamp 3 byte
//            scriptData[4] = 0x00;
//            scriptData[5] = 0x00;
//            scriptData[6] = 0x00;
//            //Extent Timestamp 1 byte
//            scriptData[7] = 0x00;
//            //StreamID 3 byte
//            scriptData[8] = 0x00;
//            scriptData[9] = 0x00;
//            scriptData[10] = 0x00;
//
//            String onMetaData = "onMetaData";
//            scriptData[11] = 0x02;
//            scriptData[12] = 0x00;
//            scriptData[13] = 0x0a;
//            System.arraycopy(onMetaData.getBytes(),0,scriptData,14,onMetaData.length());
//
//            scriptData[14+onMetaData.length()] = 0x08;////ECMAARRAY


            out.write(demo);
            out.write(0x00);
            out.write(0x00);
            out.write(0x00);
            out.write(0xdf);

            out.flush();

        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public void writeFrame(byte[] data){
        try{
            //write header
            out.write(0x09);
            out.write((byte)(data.length>>16&0xff));
            out.write((byte)(data.length>>8&0xff));
            out.write((byte)(data.length&0xff));
            int time = (int)System.currentTimeMillis();
            out.write((byte)(time>>16&0xff));
            out.write((byte)(time>>8&0xff));
            out.write((byte)(time>>0&0xff));
            out.write((byte)(time>>24&0xff));
            out.write(0x00);
            out.write(0x00);
            out.write(0x00);

            out.write(data);
            out.write((byte)((data.length+11)>>24&0xff));
            out.write((byte)((data.length+11)>>16&0xff));
            out.write((byte)((data.length+11)>>8&0xff));
            out.write((byte)((data.length+11)&0xff));

            out.flush();
        }catch(Exception e){
            e.printStackTrace();
        }


    }



}
